//
//  ZCOrderCanselResionVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/3/2.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCOrderCanselResionVC.h"
#import "ZCOrderCanselResionCell.h"
#import "TBCityIconFont.h"
@interface ZCOrderCanselResionVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@property (nonatomic, copy) NSDictionary *selectedInfo;

@end

@implementation ZCOrderCanselResionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e61e", 18, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.closeBtn setImage:img forState:UIControlStateNormal];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCOrderCanselResionCell" bundle:nil]
         forCellReuseIdentifier:@"ZCOrderCanselResionCell"];
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){0,0,ScreenWidth,80}];
    UINib *nib = [UINib nibWithNibName:@"ZCOrderCanselResionHeadView" bundle:nil];
    UIView *view = [nib instantiateWithOwner:self options:nil].firstObject;
    view.frame = headerView.bounds;
    [headerView addSubview:view];
    self.tableView.tableHeaderView = headerView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.tableView.backgroundColor = [UIColor whiteColor];
    });
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    ZCOrderCanselResionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCOrderCanselResionCell"
                                                                    forIndexPath:indexPath];
    cell.dict = self.items[row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UINib *nib = [UINib nibWithNibName:@"ZCOrderCanselResionVCSectionHeadView" bundle:nil];
    UIView *view = [nib instantiateWithOwner:self options:nil].firstObject;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    self.selectedInfo = self.items[row];
}

// MARK: - Action

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)doneBtnClickAction:(id)sender {
    if (self.selectedInfo == nil) {
        [self.view makeToast:@"请选择取消订单的原因" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.doneCallback) {
            self.doneCallback(self.selectedInfo);
        }
    }];
}

- (IBAction)closeBtnClickAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
